import { Debounce } from "../utils/debounce"
export class keyEvent {
    _editor
    _container
    private _anchorOffset!: number
    private _focusOffset!: number
    constructor(editor: any, container: HTMLElement) {
        this._editor = editor
        this._container = container
    }
    insertText(e: InputEvent) {
        const selection = this.getSelection()
        if (selection) {
            const selectStr = selection.toString()
            let left = ''
            if (this._anchorOffset > 0) {
                left = this._container.innerHTML.slice(0, this._anchorOffset)
            }
            const right = this._container.innerHTML.slice(this._anchorOffset)
            if (selectStr) {
                if (e.data) {
                    this._container.innerHTML = this._container.innerHTML.replace(selectStr, e.data)
                }
            } else {
                this._container.innerHTML = left + e.data + right
            }
            //设置光标位置
            selection.collapse(this._container.childNodes[0], this._anchorOffset + 1)
        }
    }
    deletText() {
        const selection = this.getSelection()
        if (selection) {
            const selectStr = selection.toString()
            if (!selectStr) {
                //单个删除
                let left = ''
                if (this._anchorOffset > 0) {
                    left = this._container.innerHTML.slice(0, this._anchorOffset)
                }
                const right = this._container.innerHTML.slice(this._anchorOffset + 1)
                this._container.innerHTML = left + right
                selection.collapse(this._container.childNodes[0], this._anchorOffset + 1)
            } else {
                //删除选中
                selection.deleteFromDocument()
            }
        }
    }
    getSelection() {
        const domSelection = window.getSelection()
        return domSelection
    }
    selectionchangeHandler() {
        const domSelection = this.getSelection()
        if (domSelection) {
            this._anchorOffset = domSelection.anchorOffset //光标起始选中位置
            this._focusOffset = domSelection.focusOffset //光标末尾位置
            if (this._anchorOffset > this._focusOffset) {
                this._anchorOffset = this._focusOffset
            }
        }
    }
    selectionchange() {
        const fn = Debounce.use(this.selectionchangeHandler.bind(this), 0)
        window.document.addEventListener('selectionchange', fn)
    }
    beforeInputHandler() {
        this._container.addEventListener("beforeinput", (e: InputEvent) => {
            e.preventDefault();
            const inputType = e.inputType
            //     if (e.inputType == "insertText" && /[a-zA-Z]/.test(e.data)) {
            //     } else if (e.inputType == "deleteContentBackward") {
            //         // deletText(e)
            //     }
            switch (inputType) {
                case 'insertText':
                    this.insertText(e)
                    break;
                case 'deleteContentBackward':
                    this.insertText(e)
                    break;
            }
        })
    }
}